package org.idea.spring.ioc.jndi.interfaces.impl;

import org.idea.spring.ioc.jndi.interfaces.DBService;

import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.Referenceable;
import javax.naming.StringRefAddr;

/**
 * @Author idea
 * @Date created in 3:08 下午 2020/4/19
 */
public class SimpleDBService implements DBService, Referenceable {

    public String location = "jdbc://location:3306/test-db";

    public String status = "start";

    @Override
    public String getLocation() {
        return location;
    }

    @Override
    public String getState() {
        return status;
    }

    @Override
    public void accessDB() {
        if (status.equals("start")) {
            System.out.println("we are accessing DB.");
        } else {
            System.out.println("DB is not start.");
        }
    }

    @Override
    public void setProperty(int index, String property) {
        if (index == 0) {
            location = property;
        } else {
            status = property;
        }
    }

    @Override
    public Reference getReference() throws NamingException {
        Reference ref = new Reference(getClass().getName(), SimpleDBServiceFactory.class.getName(), null);
        ref.add(new StringRefAddr("location", location));
        ref.add(new StringRefAddr("state", status));
        return ref;
    }
}
